package com.taichuan.selfcheck.mvp.presenter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;
import com.orhanobut.logger.Logger;
import com.taichuan.mvp.base.MvpBasePresenter;
import com.taichuan.selfcheck.entity.USBDeviceInfo;
import com.taichuan.selfcheck.mvp.viewinterface.UsbDeviceInterface;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by CamelLuo on 2018/6/21.
 */
public class USBDevicePresenter extends MvpBasePresenter<UsbDeviceInterface> {
    private static final String TAG = USBDevicePresenter.class.getSimpleName();

    private USBBroadcastReceiver usbBroadcastReceiver;
    private Context context;

    public USBDevicePresenter(Context context) {
        this.context = context;
    }

    @Override
    public void attachView(UsbDeviceInterface view) {
        super.attachView(view);
        registerUsbReceiver();
    }

    @Override
    public void detachView() {
        super.detachView();
        if (usbBroadcastReceiver != null){
            context.unregisterReceiver(usbBroadcastReceiver);
        }
    }

    private void registerUsbReceiver(){
        //注册广播接收者
        usbBroadcastReceiver = new USBBroadcastReceiver();
        IntentFilter usbDeviceStateFilter = new IntentFilter();
        // 2. 设置接收广播的类型
        usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        context.registerReceiver(usbBroadcastReceiver, usbDeviceStateFilter);
    }

    public class USBBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // 区分接收到的是哪个广播
            String action = intent.getAction();

            if (action != null) {
                Logger.t(TAG).e("==============接收到了 USB管理器广播，action为：  " + intent.getAction());

                switch (action) {
                    case UsbManager.ACTION_USB_DEVICE_ATTACHED: {                 //接收到U盘设备插入广播
                        Log.e(TAG, "onReceive: 检测到USB设备插入 广播");
                        //读取设备中所插入的所有USB设备（包括USB摄像头、USB网卡等）
                        readAllUsbDevice();
                        break;
                    }
                    case UsbManager.ACTION_USB_DEVICE_DETACHED: {                   //接收到U盘设设备拔出广播
                        Log.e(TAG, "onReceive: USB设备拔出广播");

                        //读取设备中所插入的所有USB设备（包括USB摄像头、USB网卡等）
                        readAllUsbDevice();
                        break;
                    }
                }
            }
        }
    }

    /**
     * 定义一个方法用于读取设备中所插入的所有USB设备（包括USB摄像头、USB网卡等）
     */
    public void readAllUsbDevice() {
        UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
        if (deviceList != null && deviceList.size() > 0){
            //创建List用于存储USB设备信息
            List<USBDeviceInfo> usbDeviceList = new ArrayList<>();

            for (Map.Entry<String, UsbDevice> usbDeviceEntity : deviceList.entrySet()) {
                UsbDevice usbDevice = usbDeviceEntity.getValue();
                Log.e(TAG, "onReceive: 当前插入的 USB设备的挂载路径： " + usbDeviceEntity.getKey() +
                        " | 设备信息： " + usbDevice.toString());
                //生成实体
                USBDeviceInfo usbDeviceInfo = new USBDeviceInfo(usbDevice);
                usbDeviceList.add(usbDeviceInfo);
            }

            //发送给UI进行刷新
            getView().refreshUsbDeviceInfo(usbDeviceList);
        }

    }
}
